﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace BST250301TestStand.Models
{
    public class AppConfig
    {
        public string DBConnectionServer { get; set; }
        public string PLCType { get; set; }
        public string PLCIP { get; set; }
        public int PLCPort { get; set; }
        public string NVHIP { get; set; }
        public int NVHPort { get; set; }
        /// <summary>
        /// NVH的记录文件保存路径
        /// </summary>
        public string NVHPath { get; set; }
        public string AppVersion { get; set; }
        
        public string AppAuthor { get; set; }
        public string AppName { get; set; }
        public string AppSubname { get; set; }
        public string AppAuthorMail { get; set; }
        
        public string LoginUser { get; set; }

        public int DBPort { get; set; }
        public string Notes { get; set; }
        public string Warning { get; set; }

        /// <summary>
        /// 接收TCU地址组1
        /// </summary>
        public string TCU1 { get; set; }
        /// <summary>
        /// 接收TCU地址组2
        /// </summary>
        public string TCU2 { get; set; }
        /// <summary>
        /// 接收TCU地址组2
        /// </summary>
        public string TCU3 { get; set; }
        /// <summary>
        /// 接收TCU地址组4
        /// </summary>
        public string TCU4 { get; set; }
        /// <summary>
        /// 发送TCU地址组1
        /// </summary>
        public string HostSys1 { get; set; }
        /// <summary>
        /// 发送TCU地址组2
        /// </summary>
        public string HostSys2 { get; set; }
        /// <summary>
        /// 动作离合允许起步速度rpm
        /// </summary>
        public int ClutchEN_Start { get; set; }
        /// <summary>
        /// 动作离合允许最大速度rpm
        /// </summary>
        public int ClutchEN_End { get; set; } 
        /// <summary>
        /// 记录间隔时间，ms
        /// </summary>
        public int RecordInterval { get; set; }
        /// <summary>
        /// TCU阶跃检查间隔时间，ms
        /// </summary>
        public int TCUInterval { get; set; }
        /// <summary>
        /// 滚动列表显示间隔时间，ms
        /// </summary>
        public int ShowListboxInterval { get; set; }
        /// <summary>
        /// 滤波a值
        /// </summary>
        public float FilterAlpha { get; set; }
        
        public string PlanName { get; set; }
        /// <summary>
        /// 90%稳定压力
        /// </summary>
        public float StableBar_90 { get; set; }
        /// <summary>
        /// 10%稳定压力
        /// </summary>
        public float StableBar_10 { get; set; }
        
        public AppConfig Load()
        {            
            if (!File.Exists("config.json")) 
                return new AppConfig(); // 文件不存在则返回默认配置
            string json = File.ReadAllText("config.json");
            return JsonSerializer.Deserialize<AppConfig>(json);
        }

        // 保存配置
        public void Save()
        {
            string json = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
            File.WriteAllText("config.json", json);
        }
    }
}
